package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class TPostPeer extends BasePeer{
	public static final String tableName = "TPost";
	public static final String USERID = "userId";
	public static final String POSTID = "postId";
	public static final String FOLDERID = "folderId";
	public static final String CATEGORYID = "categoryId";
	public static final String POSTTIME = "postTime";
	public static final String LASTREPLYTIME = "lastReplyTime";
	public static final String STATE = "state";
	public static final String[] columnNames=new String[]{"userId","postId","folderId","categoryId","postTime","lastReplyTime","state"};
	public static TPost getTPostByPKs(Object categoryId,Object folderId,Object postId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(TPostPeer.tableName,TPostPeer.columnNames," TRUE  AND `categoryId`=?  AND `folderId`=?  AND `postId`=? ",new Object[]{categoryId,folderId,postId,},0, 1,conn);
			TPost tPost = new TPost();
		fill(rs,tPost);
		return tPost;
	}
	public static TPost getTPostReferenceColumnsOnlyByPKs(Object categoryId,Object folderId,Object postId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(TPostPeer.tableName,new String[]{"categoryId","folderId","postId",}," TRUE AND `categoryId`=?AND `folderId`=?AND `postId`=?",new Object[]{categoryId,folderId,postId,}, 0,  1,conn);
			TPost tPost = new TPost();
		fill(rs,tPost);
		return tPost;
	}
	public static TPost getTPostByPostId(int postId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(TPostPeer.tableName,TPostPeer.columnNames,"`postId`=?",new Object[]{postId},0, 1,conn);
			TPost tPost = new TPost();
		fill(rs,tPost);
		return tPost;
	}
	public static TPost getTPostByFolderId(int folderId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(TPostPeer.tableName,TPostPeer.columnNames,"`folderId`=?",new Object[]{folderId},0, 1,conn);
			TPost tPost = new TPost();
		fill(rs,tPost);
		return tPost;
	}
	public static TPost getTPostByCategoryId(int categoryId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(TPostPeer.tableName,TPostPeer.columnNames,"`categoryId`=?",new Object[]{categoryId},0, 1,conn);
			TPost tPost = new TPost();
		fill(rs,tPost);
		return tPost;
	}
	public static void fill(ResultSet rs, TPost tPost) throws SQLException {
		if(rs.next()){
			try{tPost.setUserId(rs.getInt("userId"));}catch(Exception ex){}
			try{tPost.setPostId(rs.getInt("postId"));}catch(Exception ex){}
			try{tPost.setFolderId(rs.getInt("folderId"));}catch(Exception ex){}
			try{tPost.setCategoryId(rs.getInt("categoryId"));}catch(Exception ex){}
			try{tPost.setPostTime(rs.getTimestamp("postTime"));}catch(Exception ex){}
			try{tPost.setLastReplyTime(rs.getTimestamp("lastReplyTime"));}catch(Exception ex){}
			try{tPost.setState(rs.getLong("state"));}catch(Exception ex){}
			tPost.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<TPost> tPostCollection) throws SQLException {
		while(rs.next()){
			TPost tPost=new TPost();
			try{tPost.setUserId(rs.getInt("userId"));}catch(Exception ex){}
			try{tPost.setPostId(rs.getInt("postId"));}catch(Exception ex){}
			try{tPost.setFolderId(rs.getInt("folderId"));}catch(Exception ex){}
			try{tPost.setCategoryId(rs.getInt("categoryId"));}catch(Exception ex){}
			try{tPost.setPostTime(rs.getTimestamp("postTime"));}catch(Exception ex){}
			try{tPost.setLastReplyTime(rs.getTimestamp("lastReplyTime"));}catch(Exception ex){}
			try{tPost.setState(rs.getLong("state"));}catch(Exception ex){}
			tPost.resetModified();
			tPostCollection.add(tPost);
		}
	}
	public static boolean update(TPost tPost, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < TPostPeer.columnNames.length; i++) {
			if (((TPostBase)tPost).modified[i]) {
				columns.add(TPostPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `TPost` SET ");
		sql.append("WHERE TRUE AND `categoryId` = ? AND `folderId` = ? AND `postId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,tPost.get(column));
		}
		pstmt.setObject(columns.size()+1+0,tPost.get("categoryId"));
		pstmt.setObject(columns.size()+1+1,tPost.get("folderId"));
		pstmt.setObject(columns.size()+1+2,tPost.get("postId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(TPost tPost, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (tPost.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `TPost` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (tPost.get(column) != null) {
				pstmt.setObject(i + 1 + j, tPost.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<TPost> getTPostCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<TPost> collection=new Vector<TPost>();
		fill(rs,collection);
		return collection;
	}
}
